#include <iostream>
#include <vector>

#define STB_IMAGE_IMPLEMENTATION
#include <GL.h>
#include "RubikCube.h"
using namespace GLM;

float alpha = 0;
float deltaTime = 0.1;
bool firstMouse = true;
double lastX, lastY;

Camera camera(Vector4(10, 0, 0, 1.0));

RubikCube *rubikCube;

DWORD startTime = GetTickCount();

void draw(GLWindow &window)
{
    alpha += 1.0f / 60;
    Graphic graphic = window.createGraphic();
    if (window.getKeyState(VK_UP))
        camera.ProcessKeyboard(FORWARD, deltaTime);
    if (window.getKeyState(VK_DOWN))
        camera.ProcessKeyboard(BACKWARD, deltaTime);
    if (window.getKeyState(VK_LEFT))
        camera.ProcessKeyboard(LEFT, deltaTime);
    if (window.getKeyState(VK_RIGHT))
        camera.ProcessKeyboard(RIGHT, deltaTime);

    if (window.getKeyState('Q'))
        rubikCube->startRotate(0, 0, 1);
    if (window.getKeyState('W'))
        rubikCube->startRotate(0, 1, 1);
    if (window.getKeyState('E'))
        rubikCube->startRotate(0, 2, 1);

    if (window.getKeyState('A'))
        rubikCube->startRotate(1, 0, 1);
    if (window.getKeyState('S'))
        rubikCube->startRotate(1, 1, 1);
    if (window.getKeyState('D'))
        rubikCube->startRotate(1, 2, 1);

    if (window.getKeyState('Z'))
        rubikCube->startRotate(2, 0, 1);
    if (window.getKeyState('X'))
        rubikCube->startRotate(2, 1, 1);
    if (window.getKeyState('C'))
        rubikCube->startRotate(2, 2, 1);

    if (window.getKeyState('I'))
        rubikCube->startRotate(0, 0, -1);
    if (window.getKeyState('O'))
        rubikCube->startRotate(0, 1, -1);
    if (window.getKeyState('P'))
        rubikCube->startRotate(0, 2, -1);

    if (window.getKeyState('J'))
        rubikCube->startRotate(1, 0, -1);
    if (window.getKeyState('K'))
        rubikCube->startRotate(1, 1, -1);
    if (window.getKeyState('L'))
        rubikCube->startRotate(1, 2, -1);

    if (window.getKeyState('B'))
        rubikCube->startRotate(2, 0, -1);
    if (window.getKeyState('N'))
        rubikCube->startRotate(2, 1, -1);
    if (window.getKeyState('M'))
        rubikCube->startRotate(2, 2, -1);
    Matrix4f view = camera.getViewMatrix();
    window.setView(view);
    Vector4 v(-1, -0.5, 1, 1.0);

    DWORD now = GetTickCount();
    float delta = 1.0f * (now - startTime) / 1000 * PI/2;
    startTime = now;
    rubikCube->rotate(delta);
    rubikCube->draw(graphic);
    

}
void mouse_callback(double xpos, double ypos)
{
    //如果为第一次进入 记录当前鼠标位置 为初始化鼠标位置
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }
    //计算当前帧和上一帧的位置差
    double xoffset = xpos - lastX;
    double yoffset = lastY - ypos; // 这里如果不反过来 实际操作过程中向上移动摄像机 物体也会跟着向上移动 实际应该向下
    lastX = xpos;
    lastY = ypos;
    camera.ProcessMouseMovement(-yoffset, -xoffset);
    
}
int main()
{

    GLWindow window;
    
    TCHAR *title = _T("RubikCube - Soft rendering")
                   _T(" press key:QWE-ASD-ZXC IOP-JKL-BNM");
    window.create(800, 600, title);
    
    ShowCursor(false);
    window.onDraw = draw;
    window.onMouseMove = mouse_callback;
    rubikCube = new RubikCube(&window);
    
    window.loop();
    
    return 0;
}